//
//  FlatButton.m
//  Popping
//
//  Created by André Schneider on 12.05.14.
//  Copyright (c) 2014 André Schneider. All rights reserved.
//

#import "FlatButton.h"
#import <POP/POP.h>

@interface FlatButton()
- (void)setup;
- (void)scaleToSmall;
- (void)scaleAnimation;
- (void)scaleToDefault;
@end

@implementation FlatButton

+ (instancetype)button
{
    return [self new];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

#pragma mark - Instance methods

//- (UIEdgeInsets)titleEdgeInsets
//{
//    return UIEdgeInsetsMake(4.f,
//                            28.f,
//                            4.f,
//                            28.f);
//}
//
//- (CGSize)intrinsicContentSize
//{
//    CGSize s = [super intrinsicContentSize];
//
//    return CGSizeMake(s.width + self.titleEdgeInsets.left + self.titleEdgeInsets.right,
//                      s.height + self.titleEdgeInsets.top + self.titleEdgeInsets.bottom);
//    
//}

#pragma mark - Private instance methods

- (void)setup
{
//    self.backgroundColor = self.tintColor;
//    self.layer.cornerRadius = 4.f;
//    [self setTitleColor:[UIColor whiteColor]
//                 forState:UIControlStateNormal];
//    self.titleLabel.font = [UIFont fontWithName:@"Avenir-Medium"
//                                             size:22];
    self.showsTouchWhenHighlighted = NO;
    self.adjustsImageWhenHighlighted = NO;
    [self addTarget:self action:@selector(scaleToSmall)
   forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragEnter];
    [self addTarget:self action:@selector(scaleAnimation)
   forControlEvents:UIControlEventTouchUpInside];
    [self addTarget:self action:@selector(scaleToDefault)
   forControlEvents:UIControlEventTouchDragExit | UIControlEventTouchDragOutside];
}

- (void)scaleToSmall
{
    POPBasicAnimation *scaleAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
    scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(0.95f, 0.95f)];
    [self.layer pop_addAnimation:scaleAnimation forKey:@"layerScaleSmallAnimation"];
}

- (void)scaleAnimation
{
    POPSpringAnimation *scaleAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
    scaleAnimation.velocity = [NSValue valueWithCGSize:CGSizeMake(3.f, 3.f)];
    scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(1.f, 1.f)];
    scaleAnimation.springBounciness = 18.0f;
    [scaleAnimation setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
        if (finished) {
            POPBasicAnimation *scaleAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
            scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(1.f, 1.f)];
            [self.layer pop_addAnimation:scaleAnimation forKey:@"layerScaleDefaultAnimation"];
        }
    }];
    [self.layer pop_addAnimation:scaleAnimation forKey:@"layerScaleSpringAnimation"];
}

- (void)scaleToDefault
{
    POPBasicAnimation *scaleAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
    scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(1.f, 1.f)];
    [self.layer pop_addAnimation:scaleAnimation forKey:@"layerScaleDefaultAnimation"];
}

-  (void)shake{
    POPSpringAnimation *positionAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionX];
    positionAnimation.velocity = @2000;
    positionAnimation.springBounciness = 20;
    [positionAnimation setCompletionBlock:^(POPAnimation *animation, BOOL finished) {
        self.userInteractionEnabled = YES;
    }];
    [self.layer pop_addAnimation:positionAnimation forKey:@"positionAnimation"];
}
@end
